package com.king.zxing.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.king.zxing.app.R;
import com.king.zxing.app.dao.UserDao;

public class ForgetPasswordActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        //用户名
        EditText usernameEditText = (EditText)findViewById(R.id.usernameEditText) ;
        //密码
        EditText passwordEditText = (EditText)findViewById(R.id.passwordEditText) ;
        //确认密码
        EditText checkPasswordEditText = (EditText)findViewById(R.id.checkPasswordEditText);
        //验证码
        EditText confirmCodeText = (EditText) findViewById(R.id.confirmCode);
        //同意《服务协议》和《隐私政策》
        ImageView confirmXieYi = (ImageView) findViewById(R.id.confirm_protocol);

        //返回登录
        TextView backLogin = findViewById(R.id.back_login);
        backLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //从当前的这个界面跳转到注册界面去
                Intent intent = new Intent(ForgetPasswordActivity.this, LoginActivity.class);
                //启动intent，执行界面跳转
                startActivity(intent);
            }
        });
        //用户名提示
        TextView usernameProTxt = (TextView)findViewById(R.id.username_prompt_text);
        TextView passwordProTxt = (TextView)findViewById(R.id.password_prompt_text);
        TextView checkPasswordProTxt = (TextView)findViewById(R.id.check_password_prompt_text);
        TextView confirmCodeProTxt = (TextView)findViewById(R.id.confirm_code_confirm_text);
        TextView confirmProtocolProTxt = (TextView)findViewById(R.id.confirm_protocol_prompt);

        //false:没勾选
        final boolean[] isChecked = {false};
        //点击勾选同意协议按钮
        confirmXieYi.setOnClickListener(v -> {
            isChecked[0] = !isChecked[0];
            //如果这个勾选了
            if (isChecked[0]) {
                confirmXieYi.setImageResource(R.drawable.check);
            } else {
                confirmXieYi.setImageResource(R.drawable.unchecked);
            }
        });

        Button registBtn = (Button) findViewById(R.id.loginButton);
        //点击注册按钮
        registBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameEditText.getText().toString();
                String password = passwordEditText.getText().toString();
                String checkPassword = checkPasswordEditText.getText().toString();
                String confirmCode = confirmCodeText.getText().toString();

                usernameProTxt.setVisibility(username.isEmpty() ? View.VISIBLE : View.INVISIBLE);
                passwordProTxt.setVisibility(password.isEmpty() ? View.VISIBLE : View.INVISIBLE);
                checkPasswordProTxt.setVisibility(checkPassword.isEmpty() ? View.VISIBLE : View.INVISIBLE);
                confirmCodeProTxt.setVisibility(confirmCode.isEmpty() ? View.VISIBLE : View.INVISIBLE);
                confirmProtocolProTxt.setVisibility(!isChecked[0] ? View.VISIBLE : View.INVISIBLE);

                if(!password.equals(checkPassword)){
                    showMsg("两次输入的密码不相等！");
                } else if(!username.isEmpty() && !password.isEmpty() && !confirmCode.isEmpty() && isChecked[0]){
                    //创建一个User对象
                    long isAdded = (new UserDao(ForgetPasswordActivity.this)).addUserForRegister(username,"111" ,password);
                    if(isAdded == -1){
                        showMsg("修改密码失败");
                    }else{
                        showMsg("修改密码成功！");
                        Intent intent = new Intent(ForgetPasswordActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });
    }
    private void showMsg(CharSequence llw) {
        Toast.makeText(this, llw, Toast.LENGTH_SHORT).show();
    }
}
